
package controllers;

import entity.Users;
import java.io.Serializable;
import java.util.List;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.mail.MessagingException;
import services.implementation.ForHelp;
import services.implementation.RegistrationService;
import services.implementation.TimerSessionBean;
import services.implementation.UsersService;

/*
 * @author smile
 */
@ManagedBean(name = "userController")
@SessionScoped
public class UsersController implements Serializable {
    @EJB
    private TimerSessionBean timerSessionBean;
    @EJB
    private RegistrationService registrationService;
    @EJB
    private UsersService usersService;
    
    private String _login;
    private String _password;
    private String _email;
    private String _role;
    private int _current_user_id;
    
    public String getEmail() {
        return _email;
    }

    public void setEmail(String _email) {
        this._email = _email;
    }

    public String getLogin() {
        return _login;
    }

    public void setLogin(String _login) {
        this._login = _login;
    }

    public String getPassword() {
        return _password;
    }

    public void setPassword(String _password) {
        this._password = _password;
    }

    public String getRole() {
        return _role;
    }

    public void setRole(String _role) {
        this._role = _role;
    }

    public int getCurrent_user_id() {
        return _current_user_id;
    }

    public void setCurrent_user_id(int _current_user_id) {
        this._current_user_id = _current_user_id;
    }
    

    public List<Users> getAllUser (){
        return usersService.getAllUsers();
    }
    
    /** Creates a new instance of UsersController */
    public UsersController() {
    }
    
    /*
     * If the user does not exist return "0"
     */
    public Integer isUsersExist(){
        if (usersService.getAllUsers().isEmpty()){
            return 0;
        }
        return 1;
    }

    /*
     * Edit user. Preparing for Editing.
     * Get user by him id and fill textfield
     */
    public String edit() {
        try {
            Users user = new Users();
            user = usersService.getUserById(this._current_user_id);
            this._login = user.getLogin();
            this._password = user.getPassword();
            this._email = user.getEmail();
            this._role = user.getRoleSux();
            return "Edit";
        } catch (Exception e) {
            ForHelp.addErrorMessage(e, e.getMessage());
            return null;
        }
    }
        
    /*
     * Delete user by him id.
     */
    public String destroy() {
        try {
            Users user = new Users();            
            user = usersService.getUserById(this._current_user_id);
            usersService.deleteUser(user);
            
            ForHelp.addSuccessMessage(
                    ResourceBundle
                    .getBundle("/Bundle")
                    .getString("User.Deleted")
            );
            return "List";
        } catch (Exception e) {
            ForHelp.addErrorMessage( 
                    ResourceBundle
                    .getBundle("/Bundle")
                    .getString("Persistence.Error.Occured")
            );
            return null;
        }
    }
    
    /*
     * Add user to database.
     */
    public String create() {
        // true if email is exist
        if (registrationService.checkEmail(this._email)){
            ForHelp.addErrorMessage(ResourceBundle
                    .getBundle("/Bundle")
                    .getString("Users.Add.Email.Exist"));
            return null;
        }
                
        if(prepareCreate()){
            return "Add";
        }else{
            ForHelp.addErrorMessage(ResourceBundle
                    .getBundle("/Bundle")
                    .getString("Users.Add.Select.UserType"));
            return null;
        }
    }
    
    /*
     * Add user to database.
     */
    public boolean prepareCreate() {
        try {
            registrationService.registration(
                    this._email, 
                    this._login, 
                    this._password,
                    this._role);
            ForHelp.addSuccessMessage(ResourceBundle
                    .getBundle("/Bundle")
                    .getString("Users.Created"));
            return true;
        } catch (Exception e) {
            ForHelp.addErrorMessage(e, e.getMessage());
            return false;
        }
    }
    
    /*
     * Edit user.
     */
    public String update(){
        try {
            Users user = new Users();
            user.setId(this._current_user_id);
            user.setEmail(this._email);
            user.setLogin(this._login);
            user.setRoleSux(this._role);
            user.setPassword(this._password);
            usersService.updateUser(user);
            return "List";
        } catch (Exception e) {
            ForHelp.addErrorMessage(e, e.getMessage());
            return null;
        }
    } 
}


/*
    private Integer selectedUserTypeId;
    public Integer getSelectedUserTypeId() {
        return selectedUserTypeId;
    }

    public void setSelectedUserTypeId(Integer selectedUserTypeId) {
        this.selectedUserTypeId = selectedUserTypeId;
    }
 
 
             if (this.selectedUserTypeId.equals(-1)){
                ForHelp.addErrorMessage(ResourceBundle
                        .getBundle("/Bundle")
                        .getString("Users.Update.Error.SelectUserType"));
                return null;
            }
 
    public List<SelectItem> getUsersType() {
      List<Userstype> ustypeList = usersTypeService.getAllUsersType();
      List<SelectItem> list = new ArrayList<SelectItem>(ustypeList.size()+1);
      list.add(new SelectItem(Long.valueOf(-1L), "Select..."));
      for (Userstype ustype : ustypeList) {
         SelectItem selectItem = new SelectItem(ustype.getId(), ustype.getName());
         list.add(selectItem);
      }
      return list;
   }

 * 
                <h:outputLabel value="#{bundle['Users.Add.Label.UserType']}" 
                    for="idUsertype" />
                
                <h:selectOneMenu id="idUsertype" required="true" 
                    value="#{userController.selectedUserTypeId}" 
                    title="#{bundle['Users.Add.Title.UserType']}" 
                    requiredMessage="#{bundle['Users.Add.RM.UserType']}">
                    <f:selectItems 
                        value="#{userController.getUsersType()}"/>
                </h:selectOneMenu> 

 */